---
sidebar_position: 8
---

# Forward tunnel

Forward tunnel allows you to connect to a socket address on the device from Tango.

Unlike in Google ADB CLI, forward tunnel in Tango doesn't require associating the tunnel to a local address and connecting to that address. Instead, only the device address is needed.

```ts
interface AdbSocket {
  readonly readable: ReadableStream<Uint8Array>;
  readonly writable: WritableStream<Consumable<Uint8Array>>;
  readonly service: string;
  readonly closed: Promise<void>;
  close(): Promise<void>;
}

declare class Adb {
  createSocket(address: string): Promise<AdbSocket>;
}
```

## Close socket

Call the `close` method on the socket to close it.

If there are still unread data in the `readable` stream, the data can still and must be read. Not reading them will cause the whole connection to be blocked. Also, the `closed` promise will not be resolved until all data has been read.

If the remaining data is not needed, `stream.cancel()` (or `stream.getReader().cancel()` if using a reader) can be called to discard them.

The `closed` promise will be resolved after the device has acknowledged the close.

:::info

Not closing a socket will cause a small memory leak, and depends on the other end of the socket, it may not work correctly.

:::

## Example

```ts transpile
import { WritableStream } from "@yume-chan/stream-extra";

const socket = await adb.createSocket("tcp:1234");

await socket.readable.pipeTo(
  new WritableStream({
    write(chunk) {
      console.log(chunk);
    },
  }),
);

await socket.close();
```

:::info[Equivalent ADB command]

It doesn't work in the same way, but you can use the following command to register a forward tunnel and connect to it:

```sh
adb forward tcp:1234 tcp:1234
nc localhost 1234
```

:::
